//
//  MREventCenter.m
//  McRemoteServer
//
//  Created by Magic on 12/18/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MREventCenter.h"

@interface MREventCenter () {
    CGEventSourceRef _eventSource;
}

@end

@implementation MREventCenter

+ (MREventCenter *)sharedCenter
{
    static MREventCenter *sharedCenter;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        sharedCenter = [[MREventCenter alloc] init];
    });
    
    return sharedCenter;
}

- (id)init {
    self = [super init];
    if (self) {
        _eventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
    }
    return self;
}

- (void)dealloc {
    CFRelease(_eventSource);
    [super dealloc];
}

- (void)postKeyboardEventWithKeyCode:(CGKeyCode)keyCode 
                               flags:(CGEventFlags)flags 
                           annotated:(BOOL)annotated
{
    CGEventRef keyDownEvent = CGEventCreateKeyboardEvent(_eventSource, keyCode, YES);
    if (flags) {
        CGEventSetFlags(keyDownEvent, flags);
    }
        
    CGEventRef keyUpEvent = CGEventCreateKeyboardEvent(_eventSource, keyCode, NO);
    
    if (annotated) {
        CGEventPost(kCGAnnotatedSessionEventTap, keyDownEvent);
        CGEventPost(kCGAnnotatedSessionEventTap, keyUpEvent); 
    }
    else {
        CGEventPost(kCGSessionEventTap, keyDownEvent);
        CGEventPost(kCGSessionEventTap, keyUpEvent);
    }
    
    CFRelease(keyDownEvent);
    CFRelease(keyUpEvent);    
}

- (void)postKeyboardEventWithKeyCode:(CGKeyCode)keyCode flags:(CGEventFlags)flags
{
    CGEventRef keyDownEvent = CGEventCreateKeyboardEvent(_eventSource, keyCode, YES);
    CGEventSetFlags(keyDownEvent, flags);
    
    CGEventRef keyUpEvent = CGEventCreateKeyboardEvent(_eventSource, keyCode, NO);
    //CGEventSetFlags(keyUpEvent, flags);
    
    CGEventPost(kCGSessionEventTap, keyDownEvent);
    CGEventPost(kCGSessionEventTap, keyUpEvent);
    
    CFRelease(keyDownEvent);
    CFRelease(keyUpEvent);
}

- (void)postKeyboardEventWithKeyCode:(CGKeyCode)keyCode
{
    CGEventRef keyDownEvent = CGEventCreateKeyboardEvent(_eventSource, keyCode, YES);
    
    CGEventRef keyUpEvent = CGEventCreateKeyboardEvent(_eventSource, keyCode, NO);
    
    CGEventPost(kCGSessionEventTap, keyDownEvent);
    CGEventPost(kCGSessionEventTap, keyUpEvent);
    
    CFRelease(keyDownEvent);
    CFRelease(keyUpEvent);
}


- (CGPoint)mouseLocation
{
    CGPoint cursorPosition;
    CGEventRef event = CGEventCreate(nil);
    cursorPosition = CGEventGetLocation(event);
    CFRelease(event);
    return cursorPosition;
}

- (void)postMouseMoveEventWithOffset:(CGPoint)offset
{
    static CGPoint cursorPosition = {0,0};
    if (offset.x == 73822.0f && offset.y == 81345.0f) { //begin
        cursorPosition = [self mouseLocation];


    }
    
    NSLog(@"MouseMove(%f,%f)",offset.x,offset.y);
    
    CGPoint p = CGPointMake(cursorPosition.x+offset.x, cursorPosition.y+offset.y);
    

    CGEventRef mouseMoveEvent = CGEventCreateMouseEvent(_eventSource, kCGEventMouseMoved, p, 0);
    CGEventPost(kCGSessionEventTap, mouseMoveEvent);
    CFRelease(mouseMoveEvent);
}

- (void)postLeftMouseTapEvent
{
    CGPoint cursorPosition = [self mouseLocation];
    CGEventRef leftMouseDownEvent = CGEventCreateMouseEvent(_eventSource, kCGEventLeftMouseDown, cursorPosition, 0);
    CGEventRef leftMouseUpEvent = CGEventCreateMouseEvent(_eventSource, kCGEventLeftMouseUp, cursorPosition, 0);
    CGEventPost(kCGSessionEventTap, leftMouseDownEvent);
    CGEventPost(kCGSessionEventTap, leftMouseUpEvent);
    
    CFRelease(leftMouseDownEvent);
    CFRelease(leftMouseUpEvent);
    
}

- (void)postRightMouseTapEvent
{
    CGPoint cursorPosition = [self mouseLocation];
    CGEventRef rightMouseDownEvent = CGEventCreateMouseEvent(_eventSource, kCGEventRightMouseDown, cursorPosition, 0);
    CGEventRef rightMouseUpEvent = CGEventCreateMouseEvent(_eventSource, kCGEventRightMouseUp, cursorPosition, 0);
    CGEventPost(kCGSessionEventTap, rightMouseDownEvent);
    CGEventPost(kCGSessionEventTap, rightMouseUpEvent);
    
    CFRelease(rightMouseDownEvent);
    CFRelease(rightMouseUpEvent);
}

- (void)postScrollWheelEventWithOffset:(CGPoint)offset
{    
    CGEventRef event = CGEventCreate(_eventSource);
    CGPoint cursorPosition = [self mouseLocation];
    
    //static CGPoint cursorPosition = {0,0};
    if (offset.x == 73822.0f && offset.y == 81345.0f) { //begin
        //cursorPosition = [self mouseLocation];
        
        
    }
    
    CGEventSetType(event, kCGEventScrollWheel);
    CGEventSetLocation(event, cursorPosition);
    CGEventSetIntegerValueField(event, kCGScrollWheelEventDeltaAxis1, offset.x);
    CGEventSetIntegerValueField(event, kCGScrollWheelEventDeltaAxis2, offset.y);
    
    CGEventSetIntegerValueField(event, kCGScrollWheelEventInstantMouser, 0);
    
    
    CGEventPost(kCGSessionEventTap, event);
    CFRelease(event);
    
    NSLog(@"scroll (%f,%f)",offset.x,offset.y);
}

//- (void)launchApplicationWithPath:(NSString *)path
//{
//    
//    [[NSWorkspace sharedWorkspace] launchApplication:path];
//}

- (void)setFinderWindowMode:(MRFinderMode)mode
{
    NSAppleScript *script = nil;
    
    
    switch (mode) {
        case kColumnMode:
        {
            script = [[NSAppleScript alloc] initWithSource:
                      @"tell application \"Finder\"\n \
                      if Finder window 1 exists then\n \
                      set the current view of the front Finder window to column view\n \
                      end if\n \
                      end tell\n"];
            
            break;
        }
        case kIconMode:    
        {
            script = [[NSAppleScript alloc] initWithSource:
                      @"tell application \"Finder\"\n \
                      if Finder window 1 exists then\n \
                      set the current view of the front Finder window to icon view\n \
                      end if\n \
                      end tell\n"];
            break;
        }
        case kListMode:    
        {
            script = [[NSAppleScript alloc] initWithSource:
                      @"tell application \"Finder\"\n \
                      if Finder window 1 exists then\n \
                      set the current view of the front Finder window to list view\n \
                      end if\n \
                      end tell\n"];
            break;
        }
        case kCoverflowMode:    
        {
            script = [[NSAppleScript alloc] initWithSource:
                      @"tell application \"Finder\"\n \
                      if Finder window 1 exists then\n \
                      set the current view of the front Finder window to flow view\n \
                      end if\n \
                      end tell\n"];
            break;
        }
            
        default:
            break;
    }
    
    if (script)
    {
        NSDictionary *err;
        [script executeAndReturnError:&err];
        //if (err) {
        //    NSLog(@"%@",err);
        //}
        
        [script release];
    }
}



@end
